package com.itboye.textileexpres.myset;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.textileexpes.beans.CityBeans;
import com.itboye.textileexpes.beans.KefuBeans.CounBeans;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.base.BaseActivity;
import com.itboye.textileexpres.util.SPContants;
import com.itboye.textileexpres.util.SPUtils;
import com.itboye.textileexpres.widget.MyListView;
import com.itboye.textillexepres.db.DBManager;

/**
 * 城市列表
 * 
 * @author sy
 * 
 */
public class ActivityQuYU extends BaseActivity implements OnClickListener {
	private BaseAdapter adapter;
	private ListView country_lv_list;
	private Handler handler;
	private SQLiteDatabase database;
	private ArrayList<CityBeans> mCityNames;
	private ImageView back;
	EditText edit_inputcity;
	private ArrayList<CityBeans> arrayLitArea;// 区域列表
	private DBManager dbManager;
	public String city;
	int type = 1;// 1、直辖市 2、城市
	boolean searchType = true;// true、根据 城市搜索店铺 false、根据地区搜索店铺
	RelativeLayout relypoutGps;
	private TextView tvchina;
	String countryId;
	String title;

	String cityID;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_city;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		country_lv_list = (MyListView) findViewById(R.id.country_lv_list);
		cityID = getIntent().getStringExtra("id");
		dbManager = new DBManager(this);
		dbManager.openDateBase();
		// cityNoDBManager.openDateBase();
		database = SQLiteDatabase.openOrCreateDatabase(DBManager.DB_PATH + "/"
				+ DBManager.DB_NAME, null);
		tvchina.setText(city);
		if (city == null || city == "") {

		}

		try {
			if (cityID == null || cityID == "") {
				finish();
			} else
				mCityNames = getCityNames();
		} catch (Exception e) {
			// TODO: handle exception
		}
		// database.close();

		handler = new Handler();

		setAdapter(mCityNames);
		country_lv_list.setOnItemClickListener(new CityListOnItemClick());
	}

	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		// TODO Auto-generated method stub
		super.onActivityResult(arg0, arg1, arg2);
		// if (arg0 == 101 && arg1 == 102) {
		// afterCitySelected(arg2.getStringExtra("city"));
		// }
	}

	public static void goToCityListActivity(Context context) {
		Intent intent = new Intent(context, ActivityQuYU.class);
		context.startActivity(intent);
	}

	/**
	 * 从数据库获取城市数据
	 * 
	 * @return
	 */
	private ArrayList<CityBeans> getCityNames() {
		ArrayList<CityBeans> names = new ArrayList<CityBeans>();
		Cursor cursor = database.rawQuery(
				"SELECT * FROM common_area where father=" + cityID
						+ " ORDER BY area", null);
		for (int i = 0; i < cursor.getCount(); i++) {
			cursor.moveToPosition(i);
			CityBeans cityModel = new CityBeans();
			cityModel.setCountry_name(cursor.getString(cursor
					.getColumnIndex("area")));
			cityModel.setId(cursor.getString(cursor.getColumnIndex("father")));
			names.add(cityModel);
		}
		return names;
	}

	/**
	 * 城市列表点击事件
	 * 
	 * @author sy
	 * 
	 */
	class CityListOnItemClick implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
				long arg3) {
			if (pos == 0) {
				return;
			}
			searchType = true;
			CityBeans cityModel = (CityBeans) country_lv_list.getAdapter()
					.getItem(pos);
			city = cityModel.getCountry_name();
			Intent intent2 = new Intent("City");
			sendBroadcast(intent2);
			finish();
			toast(cityModel.getId() + "");
			SPUtils.put(ActivityQuYU.this, null, SPContants.COUNTRYNMAW, city);
			country_lv_list.setSelection(0);// 设置选择项为第一项
		}

	}

	/***
	 * @param 查询
	 *            递增数据库 String
	 *            str="SELECT * FROM common_province where id="+countryId
	 *            +" ORDER BY province";
	 * 
	 *            Cursor cursor = database.rawQuery( str, null);
	 * 
	 */

	/***
	 * 查询区域列表
	 * 
	 * @param cityName
	 * @return
	 */
	public ArrayList<CityBeans> queryArea(String cityNo) {
		Cursor cursor = null;
		ArrayList<CityBeans> arrayArea = new ArrayList<CityBeans>();
		if (database != null)
			cursor = database.query("hat_area", null, "father like ?",
					new String[] { cityNo + "%" }, null, null, null);
		if (cursor != null)
			while (cursor.moveToNext()) {
				CityBeans cityModel = new CityBeans();
				cityModel.setAreaCode(cursor.getString(cursor
						.getColumnIndexOrThrow("areaID")));
				cityModel.setCityCode(cursor.getString(cursor
						.getColumnIndexOrThrow("father")));
				cityModel.setArea(cursor.getString(cursor
						.getColumnIndexOrThrow("area")));
				arrayArea.add(cityModel);
			}
		cursor.close();
		// database.close();
		return arrayArea;
	}

	/**
	 * 为ListView设置适配器
	 * 
	 * @param list
	 */
	private void setAdapter(List<CityBeans> list) {

		if (list != null) {
			adapter = new ListAdapter(this, list);
			country_lv_list.setAdapter(adapter);
		}

	}

	/**
	 * ListViewAdapter
	 * 
	 * @author sy
	 * 
	 */
	private class ListAdapter extends BaseAdapter {
		private LayoutInflater inflater;
		private List<CityBeans> list;

		public ListAdapter(Context context, List<CityBeans> list) {

			this.inflater = LayoutInflater.from(context);
			this.list = list;

		}

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.item_city, null);
				holder = new ViewHolder();
				holder.name = (TextView) convertView.findViewById(R.id.name);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.name.setText(list.get(position).getCountry_name());
			return convertView;
		}

		private class ViewHolder {
			TextView name;
		}

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back:
			this.finish();
			break;
		default:
			break;
		}
	}

}